package com.terracloud.server.dao.util.mapping;

import com.fasterxml.jackson.core.type.TypeReference;
import com.terracloud.common.util.JacksonUtil;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author chenhainan
 * @since 2025/10/21
 */
@Converter
public class SetJsonConverter implements AttributeConverter<Set<String>, String> {
    @Override
    public String convertToDatabaseColumn(Set<String> strings) {
        if (strings == null ) {
            return "[]";
        }
        return JacksonUtil.toString(strings);
    }

    @Override
    public Set<String> convertToEntityAttribute(String s) {
        return JacksonUtil.readValue(s, new TypeReference<>() {
        });
    }
}
